using System.Collections.Generic;
using System.Linq;

namespace MySpreadsheet
{
    public class WorkSheet
    {
        private string name;
        private List<Row> rows;
        private List<ColumnInfo> columns;

        public WorkSheet(string name)
        {
            this.name = name;
            rows = new List<Row>();
            columns = new List<ColumnInfo>();
        }

        public string Name
        {
            get { return name; }
            set { name = value; }
        }

        public IList<Row> Rows
        {
            get
            {
                return rows;
            }
        }

        public IEnumerable<ColumnInfo> Columns
        {
            get
            {
                foreach (var column in columns)
                {
                    yield return column;
                }
            }
        }

        public IEnumerable<Cell> Cells
        {
            get
            {
                foreach (var row in rows)
                {
                    foreach (var cell in row.Cells)
                    {
                        yield return cell;
                    }
                }
            }
        }

        public Row AddRow()
        {
            var row = new Row();
            rows.Add(row);

            return row;
        }

        public IEnumerable<Cell> GetCellsByClass(string className)
        {
            return Cells.Where(cell => cell.HasClass(className));
        }

        public Cell GetCellById(string className)
        {
            var result = Cells.Where(cell => cell.Id == className).First();
            return result;
        }

        public ColumnInfo AddColumnInfo(int columnStartIndex, int columnEndIndex, double width)
        {
            var columnInfo = new ColumnInfo(columnStartIndex, columnEndIndex, width);
            columns.Add(columnInfo);
            
            return columnInfo;
        }
    }
}